home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / batchut / runat10.zip / RUN@.PAS < prev   
Pascal/Delphi Source File  |  1991-08-25  |  8KB  |  206 lines

  1. Program runat;
  2. { Reads a list of (e.g.,) file names from StdIn, and creates a temporary     }
  3. { batch file which will execute the command specified on run@'s command line }
  4. { for each of the file names in the list, then destroy itself.               }
  5. { Usage:  run@ < listfile "foo ^ bar" [options]                              }
  6. { For a detailed explanation, call without arguments or see proc usage below.}
  7. {                                                                            }
  8. { Free Software by TapirSoft Gisbert W.Selke, Aug 1991                       }
  9. {$A+,B-,D+,E+,F-,I-,L+,N-,O-,R-,S-,V- }
  10. {$M 16384,0,16384 }
  11.  
  12.   Uses Dos;
  13.  
  14.   Const progname  = 'Run@';
  15.         version   = '1.0';
  16.         copyright = 'Free Software by TapirSoft Gisbert W.Selke, Aug 1991';
  17.  
  18.         defaultbatch  = 'RUN@@.BAT';
  19.         defaultmarker = '^';
  20.         Tab           = #9;
  21.  
  22.   Var   batchname, fname, cmdproc, cmd, outcmd : string;
  23.         batchfile : text;
  24.         sr : SearchRec;
  25.         i : byte;
  26.         marker : char;
  27.         zkeep, zmanual, zquiet : boolean;
  28.  
  29.   Procedure abort(msg : string; ierr : byte);
  30.   { show error message and die                                               }
  31.   Begin                                                              { abort }
  32.     If msg <> '' Then writeln(progname,': ',msg);
  33.     Halt(ierr);
  34.   End;                                                               { abort }
  35.  
  36.   Procedure usage;
  37.   { show usage hints and die                                                 }
  38.   Begin                                                              { usage }
  39.     writeln('Reads a list of (e.g.,) file names from StdIn (one per line),');
  40.     writeln('writes a batch file (default name RUN@@.BAT) which will execute');
  41.     writeln('the command specified on ',progname,'''s command line once for');
  42.     writeln('each file.');
  43.     writeln('By default, the batch file is automatically executed.');
  44.     writeln;
  45.     writeln('Usage:  run@ < filelist "foo ^ bar" [options]');
  46.     writeln('where - "foo" is a valid DOS command or executable;');
  47.     writeln('      - each occurrence of ^ is replaced by the file names ',
  48.             'in turn;');
  49.     writeln('      - options are: /k : keep batch file, don''t destroy it;');
  50.     writeln('                     /m : request manual execution of batch',
  51.             'file,');
  52.     writeln('                          don''t run it automatically;');
  53.     writeln('                     /q : quiet mode, don''t echo commands;');
  54.     writeln('                     /s<char>: use different substitution ',
  55.             'marker,');
  56.     writeln('                          e.g., /s# (no spaces!);');
  57.     writeln('                     /f<name>: specify different name for batch');
  58.     writeln('                          file, e.g., /fDUMMY (no spaces!);');
  59.     writeln('      - quotes may be single or double; no internal quotes of');
  60.     writeln('        the same kind allowed.');
  61.     writeln('Use manual execution if the command called installs a TSR part,');
  62.     writeln('e.g., DOS PRINT, or modifies the environment.');
  63.     abort('',1);
  64.   End;                                                               { usage }
  65.  
  66.   Procedure init;
  67.   { read command line etc                                                    }
  68.  
  69.     Var cmdline : string;
  70.         cmdptr : ^string;
  71.         i : byte;
  72.         quote : char;
  73.         zswitch, zquoted, zmarker, zbatchname : boolean;
  74.  
  75.   Begin                                                               { init }
  76.     batchname  := '';
  77.     marker     := defaultmarker;
  78.     zkeep      := False;
  79.     zmanual    := False;
  80.     zquiet     := False;
  81.     cmdptr     := Ptr(PrefixSeg,$80);
  82.     cmdline    := cmdptr^;
  83.     cmd        := '';
  84.     zquoted    := False;
  85.     zswitch    := False;
  86.     zmarker    := False;
  87.     zbatchname := False;
  88.     For i := 1 To Length(cmdline) Do
  89.     Begin
  90.       If zquoted Then
  91.       Begin
  92.         If cmdline[i] = quote Then zquoted := False
  93.                               Else cmd := cmd + cmdline[i];
  94.       End
  95.       Else
  96.       Begin
  97.         If (cmdline[i] = '''') Or (cmdline[i] = '"') Then
  98.         Begin
  99.           zquoted := True;
  100.           quote := cmdline[i];
  101.         End
  102.         Else
  103.         Begin
  104.           If zswitch Then
  105.           Begin
  106.             Case UpCase(cmdline[i]) Of
  107.               'M' : zmanual := True;
  108.               'K' : zkeep   := True;
  109.               'Q' : zquiet  := True;
  110.               'S' : zmarker := True;
  111.               'F' : Begin
  112.                       zbatchname := True;
  113.                       batchname:= '';
  114.                     End;
  115.               Else usage;
  116.             End;
  117.             zswitch := False;
  118.           End
  119.           Else
  120.           Begin
  121.             If zbatchname Then
  122.             Begin
  123.               If (cmdline[i] = ' ') Or (cmdline[i] = Tab) Then
  124.                                                        zbatchname := false
  125.                            Else batchname := batchname + UpCase(cmdline[i]);
  126.             End
  127.             Else
  128.             Begin
  129.               If zmarker Then
  130.               Begin
  131.                 If Not ((cmdline[i] = ' ') Or (cmdline[i] = Tab)) Then
  132.                                                       marker := cmdline[i];
  133.                 zmarker := False;
  134.               End
  135.               Else
  136.               Begin
  137.                 If Not (cmdline[i] In [' ',Tab,'/','-']) Then usage;
  138.                 zswitch := (cmdline[i] = '/') Or (cmdline[i] = '-');
  139.               End;
  140.             End;
  141.           End;
  142.         End;
  143.       End;
  144.     End;
  145.     If cmd = '' Then usage;
  146.     If batchname = '' Then batchname := defaultbatch;
  147.     If Pos('.',batchname) = 0 Then batchname := batchname + '.BAT';
  148.     If Pos(marker,cmd) = 0 Then
  149.                        writeln('Warning: no substitution marker found');
  150.     cmdproc := GetEnv('COMSPEC');
  151.     If cmdproc = '' Then cmdproc := 'COMMAND.COM';
  152.   End;                                                                { init }
  153.  
  154. Begin                                                                 { main }
  155.   writeln(progname,' ',version,' -- ',copyright);
  156.   init;
  157.   If (batchname = defaultbatch) And Not (zkeep Or zmanual) Then
  158.   Begin
  159.     outcmd := '';
  160.     i := 0;
  161.     FindFirst(outcmd+batchname,AnyFile,sr);
  162.     While (DosError = 0) And (i < 255) Do
  163.     Begin
  164.       Str(i,outcmd);
  165.       FindFirst(outcmd+batchname,AnyFile,sr);
  166.       Inc(i);
  167.     End;
  168.     batchname := outcmd + batchname;
  169.   End;
  170.   Assign(batchfile,batchname);
  171.   Rewrite(batchfile);
  172.   If IOResult <> 0 Then abort('Cannot open '+batchname+' for output',3);
  173.   If zquiet Then
  174.   Begin
  175.     If Swap(DosVersion) >= $0320 Then writeln(batchfile,'@Echo Off')
  176.                                  Else writeln(batchfile,'Echo Off');
  177.     If IOResult <> 0 Then abort('Error reading input',2);
  178.   End;
  179.   Assign(input,'');
  180.   Reset(input);
  181.   While Not EoF Do
  182.   Begin
  183.     readln(fname);
  184.     If IOResult <> 0 Then abort('Error reading input',2);
  185.     outcmd := '';
  186.     For i := 1 To Length(cmd) Do
  187.     Begin
  188.       If cmd[i] = marker Then outcmd := outcmd + fname
  189.                          Else outcmd := outcmd + cmd[i];
  190.     End;
  191.     writeln(batchfile,outcmd);
  192.     If IOResult <> 0 Then abort('Error writing '+batchname,4);
  193.   End;
  194.   If Not zkeep Then write(batchfile,'Del ',batchname); { no CR/LF! }
  195.   Flush(batchfile);
  196.   Close(batchfile);
  197.   If IOResult <> 0 Then abort('Error writing '+batchname,4);
  198.   If zmanual Then writeln('Run commands now by executing      ',batchname)
  199.   Else
  200.   Begin
  201.     SwapVectors;
  202.     Exec(cmdproc,'/C ' + batchname);
  203.     SwapVectors;
  204.   End;
  205. End.
  206.